package com.zyd.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyd.module.admin.mapper.ProductImageMapper;
import com.zyd.module.admin.model.ProductImage;
import com.zyd.module.admin.service.OssService;
import com.zyd.module.admin.service.ProductImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tiezhu
 * @date 2022年4月6日
 */
@Service("productImageService")
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImage> implements ProductImageService {

    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private OssService ossService;

    @Override
    public List<ProductImage> selectByProIdList(String id) {
        QueryWrapper<ProductImage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pro_id",id);
        return productImageMapper.selectList(queryWrapper);
    }

    @Override
    public Integer removeByProIds(List<String> asList){
        QueryWrapper<ProductImage> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("pro_id",asList);
        List<ProductImage> allImage = productImageMapper.selectList(queryWrapper);
        if (allImage!=null&&allImage.size()>0){
            for (ProductImage image : allImage) {
                ossService.removeImage(image.getProImg());
            }

        }
        return  productImageMapper.delete(queryWrapper);
    }

    @Override
    public List<ProductImage> selectByImgTypeList(String proid, Integer imgType) {
        QueryWrapper<ProductImage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pro_id",proid);
        queryWrapper.eq("img_type",imgType);
        return productImageMapper.selectList(queryWrapper);
    }

    @Override
    public boolean removeFileById(String imgid) {
        ProductImage image = productImageMapper.selectById(imgid);
        ossService.removeImage(image.getProImg());
        int i = productImageMapper.deleteById(imgid);
        return i>0?true:false;
    }

    @Override
    public List<ProductImage> selectByProIdAndImgTypeIdList(String proId, Integer imgType) {
        QueryWrapper<ProductImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pro_id",proId);
        queryWrapper.eq("img_type",imgType);
        return productImageMapper.selectList(queryWrapper);
    }
}